//
//  CSYOriginBlogView.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/14.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import FFLabel

class CSYOriginBlogView: UIView,FFLabelDelegate {
    
    //记录约束属性
    private var bottomConstraints : Constraint?
    
    //数据源方法
    var messageModel : CSYMyMicroBlogModel? {
    
        didSet{
        
            iconImageView.sd_setImageWithURL(messageModel?.userMessageModel?.iconURL, placeholderImage: UIImage(named: "avatar_default_big"))
            
            verifiedImageView.image = messageModel?.userMessageModel?.verified_image
            
            rankImageView.image = messageModel?.userMessageModel?.rankImage
            
            nameLabel.text = messageModel?.userMessageModel?.name
            
            messageLabel.attributedText = CSYEmoticonManager.manager.setTextToImageText(messageModel?.text ?? "", font: messageLabel.font)
            
            print("......................\(NSDate.getSinaDateWithString(messageModel?.created_at ?? "")?.getDateDescription())")
            
            timeLabel.text = NSDate.getSinaDateWithString(messageModel?.created_at ?? "")?.getDateDescription()
            
            sourceLabel.text = "来自--\(messageModel?.getSource?.getSourceStringWithRegularExpression().sourceStr)"
            
            //设置图片显示
            pictureView.imageURLArray = messageModel?.picURLArray
            
            self.bottomConstraints?.uninstall()
            
            if let urlArray : [NSURL] = messageModel?.picURLArray where urlArray.count > 0 {
            
                pictureView.imageURLArray = urlArray
                
                pictureView.backgroundColor = UIColor.whiteColor()
                
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                    
                })
            
            }else{
                
                pictureView.hidden = true
            
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraints = make.bottom.equalTo(messageLabel.snp_bottom).offset(cellMargin).constraint
                    
                })
            
            }

        }
    
    }
    
    //构造方法
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    
    private func setUpUI(){
     
        backgroundColor = UIColor.whiteColor()
        
        //头像
        addSubview(iconImageView)
        
        //大V认证
        addSubview(verifiedImageView)
        
        //等级标识
        addSubview(rankImageView)
        
        //昵称
        addSubview(nameLabel)
        
        //发布时间
        addSubview(timeLabel)
        
        //来源
        addSubview(sourceLabel)
        
        sourceLabel.labelDelegate = self
        
        //详情
        addSubview(messageLabel)
        
        messageLabel.labelDelegate = self
        
        messageLabel.linkTextColor = UIColor.purpleColor()
        
        //图片浏览器
        addSubview(pictureView)
        
        //添加约束
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top).offset(cellMargin)
            
            make.left.equalTo(self.snp_left).offset(cellMargin)
            
            make.width.equalTo(iconWidth)
            
            make.height.equalTo(iconWidth)
            
        }
        
        iconImageView.layer.cornerRadius = 17.5
        
        iconImageView.layer.masksToBounds = true
        
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImageView.snp_right).offset(-verifiedImageView.bounds.width / 4)
            
            make.centerY.equalTo(iconImageView.snp_bottom).offset(-verifiedImageView.bounds.width / 4)
            
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconImageView.snp_right).offset(cellMargin)
            
            make.top.equalTo(iconImageView.snp_top)
            
        }
        
        rankImageView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(nameLabel.snp_right).offset(cellMargin)
            
            make.top.equalTo(nameLabel.snp_top)
            
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(nameLabel.snp_left)
            
            make.top.equalTo(nameLabel.snp_bottom).offset(5)
            
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(cellMargin)
            
            make.top.equalTo(timeLabel.snp_top)
            
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconImageView.snp_left)
            
            make.top.equalTo(iconImageView.snp_bottom).offset(cellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(messageLabel.snp_left)
            
            make.top.equalTo(messageLabel.snp_bottom).offset(cellMargin)
            
            make.width.equalTo(100)
            
            make.height.equalTo(100)
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
            
        }
        
        
    }
    
    //懒加载所有控件
    
    lazy var iconImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    lazy var verifiedImageView : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    lazy var rankImageView : UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    lazy var nameLabel : UILabel = UILabel(textInfo: "Hahahahaha", fontSize: 14, infoColor: UIColor.blackColor(), numberOFLines: 0, alphaValue: 1.0)
    
    lazy var timeLabel : UILabel = UILabel(textInfo: "11:11", fontSize: 12, infoColor: UIColor.orangeColor(), numberOFLines: 0, alphaValue: 1.0)
    
    lazy var sourceLabel : FFLabel = FFLabel(textInfo: "来自--你大爷的", fontSize: 12, infoColor: UIColor.darkGrayColor(), numberOFLines: 1, alphaValue: 1.0)
    
    lazy var messageLabel : FFLabel = FFLabel(textInfo: "这是微博的内容", fontSize: 14, numberOFLines: 0, alphaValue: 1.0 , margin: cellMargin)
    
    lazy var pictureView : CSYBlogPictureView = CSYBlogPictureView()
    
}

extension CSYOriginBlogView {

    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        print(text)
        
        if let URL : NSURL = NSURL(string: text){
            
            if text.hasPrefix("http://"){
            
                let webVC : CSYShowLinkViewController = CSYShowLinkViewController(url: URL, blogModel: messageModel!)

                self.getNavController()?.pushViewController(webVC, animated: true)
                
            }
            
        }
        
    }

}
